/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pop3proxy.persistence;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

class MemoryMailbox implements Mailbox {
    private Map<String/*uuid*/, String/*content*/> mailMap = new HashMap<>(); //uuid -> mailcontent
        
    @Override
    public List<String> listMails() {
        return new ArrayList<>(mailMap.keySet());
    }

    @Override
    public synchronized List<String> listMailsWithout(Set<String> filter) {
        List<String> uuids = new ArrayList<>();
        for(String uuid : mailMap.keySet()) {
            if (!filter.contains(uuid))
                uuids.add(uuid);
        }
        return uuids;
    }

    @Override
    public synchronized String readMail(String uuid) {
        return mailMap.get(uuid);
    }

    @Override
    public synchronized void deleteMail(String uuid) {
        mailMap.remove(uuid);
    }

    @Override
    public synchronized void writeMail(String uuid, String content) {
        mailMap.put(uuid, content);
    }

    @Override
    public int size() {
        return mailMap.keySet().size();
    }

    @Override
    public synchronized long mailSize(String uuid) {
        return mailMap.get(uuid).length();
    }

    @Override
    public boolean containsMail(String uuid) {
        return mailMap.containsKey(uuid);
    }

    @Override
    public void writeMail(String uuid, List<String> lines) {
        StringBuilder memoryMail = new StringBuilder();
        for(String line : lines)
            memoryMail.append(line).append("\r\n");
        mailMap.put(uuid, memoryMail.toString());
    }
}
